package com.testing.vip12splogin.service.impl;

import com.testing.vip12splogin.mapper.UserMapper;
import com.testing.vip12splogin.model.Result;
import com.testing.vip12splogin.model.User;
import com.testing.vip12splogin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @Classname UserServcieImpl
 * @Description 类型说明
 * @Date 2022/5/22 21:47
 * @Created by 特斯汀Roy
 */

/***
 * @Service 编写一般使用接口类名的实例化名字
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public Result login(String username, String password) {
        if (username != null && password != null) {
            if (username.length() > 2 && username.length() < 17
                    && password.length() > 2 && password.length() < 17) {
                User user = userMapper.queryUserByNameAndPwd(username, password);
                System.out.println("查询到的结果是:"+user);
                //查找到的结果有数据就是登录成功。
                if (user!=null) {
                    Result success = new Result();
                    success.setCode(200);
                    success.setMsg("恭喜登录成功");
                    return success;
                } else {
                    return new Result(-2, "用户名密码错误",
                            "输入的用户名是" + username + "密码是:" + password);
                }
            } else {
                return new Result(-3, "用户名密码长度应是3-16位！", "输入的用户名是" + username + "密码是:" + password);
            }
        } else {
            return new Result(-1, "用户名密码不能为空！",
                    "输入的用户名是" + username + "密码是:" + password);

        }
    }

    @Override
    public Result getUser(String username) {
        User user = userMapper.queryUserByName(username);
        if(user==null){
            return new Result(-1,"用户信息获取失败","数据库查询失败");
        }
        user.setUsername("");
        user.setPassword(null);
        return new Result(200,"用户信息查询成功",user);
    }

    @Override
    public Result register(String username, String password, String nickname, String description) {
        try {
            int insertCode = userMapper.insertUser(username, password, nickname, description);
            if(insertCode==1){
                return new Result(1,"注册成功",username+"用户成功注册");
            }else{
                return new Result(-1,"注册失败","用户"+username+"注册失败");
            }
        } catch (Exception e) {
            return new Result(-500,"注册失败",e.getMessage());
        }
    }

    @Override
    public Result deleteUser(String username) {
        try {
            int deleteCode = userMapper.deleteUser(username);
            if(deleteCode==1){
                return  new Result(1,"删除用户成功","已将"+username+"用户删除");
            }else{
                return new Result(-2,"删除用户失败","没有"+username+"用户");
            }
        } catch (Exception e) {
            return  new Result(-500,"删除用户失败",e.getMessage());
        }
    }
}
